package org.example;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {

    int dfs(TreeNode root, int cur) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return cur * 10 + root.val;
        }
        int lres = dfs(root.left, cur * 10 + root.val);
        int rres = dfs(root.right, cur * 10 + root.val);
        return lres + rres;
    }

    public int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}